package cn.itsource.exception;

import cn.itsource.result.JSONResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: Jason
 * @Date: 2022/8/29 21:10
 * 全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    //处理 MyException 异常
    @ExceptionHandler(MyException.class)
    public JSONResult myExceptionHandler(MyException e) {
        return JSONResult.error(e.getMessage());
    }

    //处理空指针异常
    @ExceptionHandler(NullPointerException.class)
    public JSONResult nullPointerExceptionHandler(NullPointerException e) {
        return JSONResult.error("不好，发生空指针异常" + e.getMessage());
    }

    //兜底异常
    @ExceptionHandler(Exception.class)
    public JSONResult exceptionHandler(Exception e) {
        return JSONResult.error("不好，发生异常" + e.getMessage());
    }

}